require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
#Gives an error
class TC_GroupPermissions_test < Test::Unit::TestCase
   
    def startup
      $RoleName = 'Role1'
      $DescriptionRole = 'This is a new role'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToGroupAdministration
        ie.goto($SITE_URL + 'controlpanel/HubAdmin/')
        assert(ie.contains_text('Group Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Group Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      def test_10_navigateToMemberRole 
        ie.goto($SITE_URL +'controlpanel/membership/Roles.aspx')
        assert(ie.button(:id,/AddRole$/).exists?)
        ie.button(:id,/AddRole$/).click
        assert(ie.frame(:index,1).text_field(:id,/Name$/).exists?)
        ie.frame(:index,1).text_field(:id,/Name$/).value = $RoleName
        assert(ie.frame(:index,1).text_field(:id,/Description$/).exists?)
        ie.frame(:index,1).text_field(:id,/Description$/).value = $DescriptionRole
        assert(ie.frame(:index,1).link(:id, /SaveButton$/).exists?)
        ie.frame(:index,1).link(:id, /SaveButton$/).click
      end
    def test_11_navigateToGroupPermissions 
        ie.goto($SITE_URL +'controlpanel/HubAdmin/hubpermissions.aspx')
        assert(ie.table(:id,/TaskRegion_TaskRegion_rolelist$/).exists?)
        table1 = ie.table(:id,/TaskRegion_TaskRegion_rolelist$/)
        rowcount = table1.row_count
        rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text ==$RoleName)
           table1[rowcount1][1].checkbox(:id,/TaskRegion_TaskRegion_rolelist/).set
           sleep 5
           elsif (table1[rowcount1][2].text ==$RoleName)
             table1[rowcount1][2].checkbox(:id,/TaskRegion_TaskRegion_rolelist/).set
             sleep 5
            end                                                
          rowcount1=rowcount1+1
        end
        assert(ie.link(:id,/SaveButton$/).exists?)
        sleep 5
        ie.link(:id,/SaveButton$/).click
      end
      
    def test_12_navigateToRoles
        sleep 5
        ie.goto($SITE_URL +'controlpanel/membership/Roles.aspx')
      end
      def test_13_navigateToDeleteRole
        sleep 3
        table1 = ie.table(:index, 5)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$RoleName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][3].button(:id,/DeleteButton$/).click
               break
             end   
        rowcount1=rowcount1+1
        end
        if(ok ==0)
        sleep 5
        ie.link(:id,/Next$/).click
        sleep 5
        test_13_navigateToDeleteRole
        sleep 5
      end
    end
    
    def test_99_closeIE
      ie.close
    end     
    
end